﻿/*----------------------------------------------------------------
//  <copyright file="RequestScopedServiceResolver.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2024-03-10 14:00</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.AspNetCore
{
    #region "Request 的 <see cref="ServiceLifetime.Scoped"/> 服务解析器"
    /// <summary>
    /// Request 的 <see cref="ServiceLifetime.Scoped"/> 服务解析器
    /// </summary>
    public class RequestScopedServiceResolver : IScopedServiceResolver
    {
        //字段
        private readonly IHttpContextAccessor _httpContextAccessor;

        #region "属性"
        #region "获取 是否可解析"
        /// <summary>
        /// 获取 是否可解析
        /// </summary>
        public bool ResolveEnabled => _httpContextAccessor.HttpContext != null;
        #endregion
        #region "获取 <see cref="ServiceLifetime.Scoped"/> 生命周期的服务提供者"
        /// <summary>
        /// 获取 <see cref="ServiceLifetime.Scoped"/> 生命周期的服务提供者
        /// </summary>
        public IServiceProvider ScopedProvider => _httpContextAccessor.HttpContext.RequestServices;
        #endregion

        #endregion

        #region "构造函数"
        #region "初始化一个 <see cref="RequestScopedServiceResolver"/> 类型的新实例"
        /// <summary>
        /// 初始化一个 <see cref="RequestScopedServiceResolver"/> 类型的新实例
        /// </summary>
        /// <param name="httpContextAccessor"></param>
        public RequestScopedServiceResolver(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }
        #endregion

        #endregion

        #region "方法"
        #region "获取指定服务类型的实例"
        /// <summary>
        /// 获取指定服务类型的实例
        /// </summary>
        /// <typeparam name="T">服务类型</typeparam>
        /// <returns></returns>
        public T GetService<T>()
        {
            return _httpContextAccessor.HttpContext.RequestServices.GetService<T>();
        }
        #endregion
        #region "获取指定服务类型的实例"
        /// <summary>
        /// 获取指定服务类型的实例
        /// </summary>
        /// <param name="serviceType">服务类型</param>
        /// <returns></returns>
        public object GetService(Type serviceType)
        {
            return _httpContextAccessor.HttpContext.RequestServices.GetService(serviceType);
        }
        #endregion
        #region "获取指定服务类型的所有实例"
        /// <summary>
        /// 获取指定服务类型的所有实例
        /// </summary>
        /// <typeparam name="T">服务类型</typeparam>
        /// <returns></returns>
        public IEnumerable<T> GetServices<T>()
        {
            return _httpContextAccessor.HttpContext.RequestServices.GetServices<T>();
        }
        #endregion
        #region "获取指定服务类型的所有实例"
        /// <summary>
        /// 获取指定服务类型的所有实例
        /// </summary>
        /// <param name="serviceType">服务类型</param>
        /// <returns></returns>
        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _httpContextAccessor.HttpContext.RequestServices.GetServices(serviceType);
        }
        #endregion

        #endregion

    }
    #endregion

}
